package Job;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * description:定时器操作（实现电脑定时关机）。
 * author:zcx
 * lenovo
 * 时间：2022.02.07.21.17
 */
public class J1 {
    public static void main(String[] args) {
        //指定时间输出一句  2022-02-7 21:30:30
        var t2 = new Thread(() -> {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date d = null;
            try {
                d = sdf.parse("2022-02-07 21:30:00");
            } catch (ParseException e) {
                e.printStackTrace();
            }
            long end = d.getTime();
            try {
                while (true) {
                    long now = System.currentTimeMillis();
                    if (now >= end) {
                        System.out.println("时间到");
                        //调用关机程序 shutdown /s /t
                        //shutdown /a 取消关机进程
                        Runtime run = Runtime.getRuntime();
                        run.exec("cmd /k shutdown /s /t 0");
                        break;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        t2.start();
    }
}
